{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b843fb87-5511-45e1-9e4a-cc347af3e2aa",
   "metadata": {},
   "source": [
    "# 第一次复习课"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c149f4af-d01a-4571-b420-58bf43feff42",
   "metadata": {},
   "source": [
    "# 数据的类型\n",
    "Python中数据的类型两大类,一类数值型,一类文本型  \n",
    "  \n",
    "数值型包括: int, float,bool,complex  \n",
    "   \n",
    "文本型: str  \n",
    "  \n",
    "空: None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f56bf4ef-e785-4ee0-81d5-50cc6da21791",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "a=100.\n",
    "print(type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "708fb640-d1ef-49dd-87c9-38f99e520286",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(a,float)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db0d34d7-d71d-4718-8d14-7ca5d07c0eca",
   "metadata": {},
   "source": [
    "对于复数来说，我们可以用conjugate来得到一个共轭复数。这其中，就是对象对属性（成员）的访问方法  \n",
    "对象中.属性(方法）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "383fc025-ca71-44b2-b4e5-0fbdcc72f4ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'complex'>\n",
      "(3-4j)\n"
     ]
    }
   ],
   "source": [
    "z=3+4.0j\n",
    "print(type(z))\n",
    "print(z.conjugate())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba2f299c-61cd-4eb6-aaf0-718b45a7a13f",
   "metadata": {},
   "source": [
    "# 不同类型的转换\n",
    "\n",
    "转换的优先顺序: bool,int,float,complex "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "942ccc96-3a59-448f-82d3-a2a4ab39d5fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = True\n",
    "b = 1\n",
    "a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d7705b79-a1ba-4cec-a860-4c2bd226db17",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "print(2 == True )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "38311378-d8d8-4ba2-bc88-3475da60f1df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(0 == False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db3556e9-c6e5-48bb-9db1-7e93977d2278",
   "metadata": {},
   "source": [
    "# 字符串\n",
    "\n",
    "\",\"\",\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8b8da850-6cd1-48de-bfb7-18d84cec656f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Guangzhou University'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = \"Guangzhou\"\n",
    "b = \"University\"\n",
    "a + \" \" + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b92fafa4-4ad1-4c7a-9b9b-eac795bca3fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'GuangzhouGuangzhou'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c=2\n",
    "a*2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "020f7048-06b0-452b-8b58-7fe0dd5ac9aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0c5c3ca9-756e-4a52-a9f3-e275cdf2353a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"zhou\" in a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97ea6905-d0d9-44e0-b3d6-fa2034d58c1e",
   "metadata": {},
   "source": [
    "**序列的分片**\n",
    "\n",
    "除了使用序号来访问某个位置的字符外，还可以使用切片的方式获得从指定序号到某个序号之间的子字符串，同时还提供了跳着选择的方法，切片的语法有如下：\n",
    "```\n",
    "seq[start]\n",
    "seq[start:end]\n",
    "seq[start:end:step]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b147a144-0964-4427-82f5-5a6a3f98ba92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ve'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#university\n",
    "b[3:5]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "304b216f-9ad1-43ba-85d3-8521fbd17f38",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ive'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[2:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3fd91fe-0f3e-404d-8682-58583b398791",
   "metadata": {},
   "source": [
    "## 格式化输出\n",
    "\n",
    "%-c语言输出方式\n",
    "\n",
    "fString"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21487b8b-5795-466d-bd38-4b98990d44cd",
   "metadata": {},
   "source": [
    "## %输出"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2773a3c8-4b2f-4d3c-b598-5297586503c3",
   "metadata": {},
   "source": [
    "\n",
    "%f ——保留小数点后面六位有效数字  \n",
    "　　%.3f，保留3位小数位  \n",
    "%e ——保留小数点后面六位有效数字，指数形式输出  \n",
    "　　%.3e，保留3位小数位，使用科学计数法  \n",
    "%g ——在保证六位有效数字的前提下，使用小数方式，否则使用科学计数法  \n",
    "　　%.3g，保留3位有效数字，使用小数或科学计数法  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1152bb3-7d89-4f27-889f-13035eb0f103",
   "metadata": {},
   "source": [
    " 1 >>> print('%f' % 1.11)  # 默认保留6位小数  \n",
    " 2 1.110000  \n",
    " 3 >>> print('%.1f' % 1.11)  # 取1位小数  \n",
    " 4 1.1  \n",
    " 5 >>> print('%e' % 1.11)  # 默认6位小数，用科学计数法  \n",
    " 6 1.110000e+00  \n",
    " 7 >>> print('%.3e' % 1.11)  # 取3位小数，用科学计数法  \n",
    " 8 1.110e+00  \n",
    " 9 >>> print('%g' % 1111.1111)  # 默认6位有效数字  \n",
    "10 1111.11  \n",
    "11 >>> print('%.7g' % 1111.1111)  # 取7位有效数字  \n",
    "12 1111.111   \n",
    "13 >>> print('%.2g' % 1111.1111)  # 取2位有效数字，自动转换为科学计数法  \n",
    "14 1.1e+03"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "2b71f381-8cd6-4b25-80fc-05e026af0f89",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.234568\n",
      "0001.23457\n",
      "0234.12346\n"
     ]
    }
   ],
   "source": [
    "a=1.234567890\n",
    "b=234.12345678\n",
    "print(\"%f\"%a)\n",
    "print(\"%010.5f\"%a) #10代表长度0表示没数字的用0填充\n",
    "print(\"%010.5f\"%b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c9e4b3b2-5874-4f75-a08a-f2d8e5a94f28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "00100\n"
     ]
    }
   ],
   "source": [
    "a = 100\n",
    "print(\"%05d\"%a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68361469-685e-44ca-a2d8-97782c65999d",
   "metadata": {},
   "source": [
    "# 字符串输出"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56af9550-fc3d-4872-af6e-1632a02d6ffc",
   "metadata": {},
   "source": [
    "%s  \n",
    "%10s——右对齐，占位符10位  \n",
    "%-10s——左对齐，占位符10位  \n",
    "%.2s——截取2位字符串  \n",
    "%10.2s——10位占位符，截取两位字符串  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "b90a2ac3-21c7-40b1-a2bb-4ce67a2df7ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "               Guang\n"
     ]
    }
   ],
   "source": [
    "a= \"Guangzhou University\"\n",
    "print(\"%20.5s\"%a) #截5位但长度是20"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e5b0118-4666-4c2d-954a-3242d16980a0",
   "metadata": {},
   "source": [
    "# format用法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d78423f8-2c06-4b9c-b9e4-a1c256e4d9a5",
   "metadata": {},
   "source": [
    " 1 >>> print('{} {}'.format('hello','world'))  # 不带字段  \n",
    " 2 hello world  \n",
    " 3 >>> print('{0} {1}'.format('hello','world'))  # 带数字编号  \n",
    " 4 hello world  \n",
    " 5 >>> print('{0} {1} {0}'.format('hello','world'))  # 打乱顺序  \n",
    " 6 hello world hello  \n",
    " 7 >>> print('{1} {1} {0}'.format('hello','world'))  \n",
    " 8 world world hello  \n",
    " 9 >>> print('{a} {tom} {a}'.format(tom='hello',a='world'))  # 带关键字  \n",
    "10 world hello world  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "f4b56853-9235-463b-b9b0-6147de3cd9bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.234568\n",
      "0001.23457\n",
      "0234.12346\n",
      "    1.2346\n",
      "0234.123460001.23457\n",
      "0001.234570234.12346\n",
      "234.12345678\n"
     ]
    }
   ],
   "source": [
    "a=1.234567890\n",
    "b=234.12345678\n",
    "print(\"%f\"%a)\n",
    "print(\"%010.5f\"%a) #10代表长度0表示没数字的用0填充\n",
    "print(\"%010.5f\"%b)\n",
    "print(\"{:10.5}\".format(a))\n",
    "print(\"{1:010.5f}{0:010.5f}\".format(a,b))\n",
    "print(\"{0:010.5f}{1:010.5f}\".format(a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "de86a0e5-abad-414a-ae73-867bcd9b67c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0234.12346\n"
     ]
    }
   ],
   "source": [
    "print(f\"{b:010.5f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61e5e8fc-bcc4-482e-9bfe-59c1058cb942",
   "metadata": {},
   "source": [
    "请生成100个随机数（ 0, 100)浮点数，进行格式化输出，格式为：小数点后4位，小数点前3位。\n",
    "\n",
    "请输出一行10个数字，共10行。\n",
    "\n",
    "要求：10分钟内完成\n",
    "\n",
    "基础知识要求：随机数，循环语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "774415ce-e718-43a2-bc61-cd2d84d6d758",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "002.5080 047.0433 015.6594 012.6428 039.0006 029.6392 061.2305 053.1393 013.2818 033.8029 \n",
      "\n",
      "030.4320 010.1343 037.4613 006.3411 084.8722 036.1911 027.2923 022.9315 099.1591 016.5007 \n",
      "\n",
      "020.5693 058.2040 091.1801 018.2158 075.1344 085.1621 096.6726 055.8941 003.0503 013.6428 \n",
      "\n",
      "046.1652 003.0852 059.9659 054.8891 081.7286 078.8536 024.6042 025.8449 081.2986 026.3876 \n",
      "\n",
      "091.5236 002.6859 015.0789 093.2873 012.0568 077.8312 004.9404 025.7513 065.2643 082.3276 \n",
      "\n",
      "057.7429 091.0640 009.1318 015.1629 085.9279 003.7495 066.3883 031.6584 013.2931 011.8056 \n",
      "\n",
      "035.8338 006.5634 080.5588 039.1935 090.3138 028.7574 014.3080 022.1405 027.7456 090.7547 \n",
      "\n",
      "079.0849 044.5876 019.8079 054.9618 081.5298 096.5207 055.4390 014.2082 026.2067 054.5727 \n",
      "\n",
      "053.9544 067.7605 022.7641 081.8940 061.1227 017.9101 027.8558 062.6900 030.9123 051.5927 \n",
      "\n",
      "018.1717 016.5998 071.9806 005.9255 036.8441 051.3141 096.1555 028.2531 020.4779 003.8695 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "for i in range(0,100):\n",
    "    a = random.uniform(0,100)\n",
    "    print(\"%08.4f\"%a,end=\" \")\n",
    "    if (i+1)%10 ==0:\n",
    "        print(\"\\n\")\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ab901729-2b09-4f5f-8fd6-0dd018790c21",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
